package com.jml.service;

import com.jml.produce.SyncProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Slf4j
public class OrderProducerService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/sendMsg")
    public TransactionSendResult sendMsg() {
        String orderId = System.currentTimeMillis() + "";
        // 1.先投递一个半消息到MQ中
        log.info("【发送消息】：{}", orderId);
        Message<String> message = MessageBuilder.withPayload(orderId).build();
        TransactionSendResult msg = rocketMQTemplate.sendMessageInTransaction
                ("spring_boot_producer_group", "jml-topic2022", message, null);
        log.info("【发送状态】：{}", msg.getLocalTransactionState());
        log.info(">>msg:{}<<<", msg);
        return msg;
    }
}
